home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / picture / pcscrn.c < prev    next >
C/C++ Source or Header  |  1993-09-23  |  8KB  |  266 lines

  1. //    Copyright 1993 Ralph Gonzalez
  2.  
  3. /*
  4. *    FILE:        pcscrn.c
  5. *    AUTHOR:        Norman Gaskill
  6. *    CREATED:    Dec. 16, 1990
  7. */
  8.  
  9. //--------NOTE: file EGAVGA.BGI must be in current directory--------------
  10.  
  11. # include <stdio.h>
  12. # define __COLORS         //for THINK_C
  13. # include <conio.h>
  14. # include <graphics.h>    //BORLAND Turbo C++ library of graphics functions
  15. # undef __COLORS          //for THINK_C
  16. # define  LIGHTGRAY  7    //for use by floodfill
  17.  
  18. int current_window = -1;         //keeps track of current window number
  19. boolean window_defined = FALSE;  //valid window variable
  20. int win_data [MAX_WINDOWS] [6];  //window data array
  21.  
  22. # include    "pcscrn.h"
  23. # include   "error.h"
  24. extern Error    *gerror;
  25.  
  26. /******************************************************************
  27. * You must call constructor at the beginning of main().  
  28. ******************************************************************/
  29. PC_Screen::PC_Screen(void)
  30. {
  31.   int x,y,width,height;
  32.   int graphdriver = DETECT, graphmode, error_code;
  33.  
  34.   initgraph(&graphdriver, &graphmode, "..\\bgi"); //Initialize graphics,
  35.   error_code = graphresult();                     //system must be EGA or VGA
  36.   if (error_code != grOk)                         //If no graphics
  37.     return(FALSE);                                //hardware found is found
  38.   if ((graphdriver != EGA) && (graphdriver != VGA))
  39.   {
  40.     closegraph();
  41.     return FALSE;
  42.   }
  43.   width = getmaxx();
  44.   height = getmaxy();
  45.   x = width/2;
  46.   y = height/2;
  47.   device_frame->set(x,y,width,-height);
  48.   normalized_frame->height = normalized_frame->width / 
  49.                              get_device_aspect_ratio();
  50. }
  51.  
  52. /******************************************************************
  53. * Add new window to screen
  54. ******************************************************************/
  55. int PC_Screen::new_window(Frame *frame)
  56. {
  57.   int left,
  58.       right,
  59.       top,
  60.       bottom;
  61.   Coord2  *old_pt,
  62.           *new_pt;
  63.   
  64.   old_pt = new Coord2;
  65.   new_pt = new Coord2;
  66.   
  67.   old_pt->set(frame->x-frame->width/2.,frame->y-frame->height/2.);
  68.   new_pt->convert(old_pt,normalized_frame,device_frame);
  69.   left = new_pt->x;
  70.   bottom = new_pt->y;
  71.   
  72.   old_pt->set(frame->x+frame->width/2.,frame->y+frame->height/2.);
  73.   new_pt->convert(old_pt,normalized_frame,device_frame);
  74.   right = new_pt->x;
  75.   top = new_pt->y;
  76.   
  77.   delete old_pt;
  78.   delete new_pt;
  79.  
  80.   if (num_windows < MAX_WINDOWS)
  81.   {
  82.     setviewport(left,top,right,bottom,1); //1=truncate output
  83.     win_data[num_windows][0] = left;
  84.     win_data[num_windows][1] = top;
  85.     win_data[num_windows][2] = right;
  86.     win_data[num_windows][3] = bottom;
  87.     current_window = num_windows;
  88.     num_windows++;
  89.     return num_windows-1;
  90.   }
  91.   else
  92.   {
  93.     gerror->report("Ran out of windows");
  94.     return num_windows;
  95.   }
  96. }
  97.  
  98. /******************************************************************
  99. * Bring window to front.
  100. ******************************************************************/
  101. void  PC_Screen::make_closest(int window_num)
  102. {
  103.   if (window_num > -1 && window_num < num_windows)
  104.   {
  105.     current_window = window_num;
  106.     setviewport(win_data[current_window][0],      //resets viewport
  107.                 win_data[current_window][1],
  108.                 win_data[current_window][2],
  109.                 win_data[current_window][3],1);   //1=truncate output
  110.     setfillstyle(SOLID_FILL, win_data[current_window][4]);  //set and
  111.     floodfill(2,2,LIGHTGRAY);                     //flood fill current window
  112.     setcolor(win_data[current_window][5]);        //reset draw color
  113.   }
  114.   else
  115.     gerror->report("Illegal window number");
  116. }
  117.  
  118. /******************************************************************
  119. * Get coordinate frame of window in device coordinates.
  120. ******************************************************************/
  121. void  PC_Screen::get_window_device_frame(int window_num,Frame *frame)
  122. {
  123.   int x,
  124.       y,  
  125.       width,
  126.       height;
  127.   struct viewporttype viewinfo;
  128.  
  129.   if (window_num > -1 && window_num < num_windows)
  130.   {
  131.     getviewsettings(&viewinfo);
  132.     width = viewinfo.right - viewinfo.left;
  133.     height = viewinfo.bottom - viewinfo.top;
  134.     x = width/2;
  135.     y = height/2;
  136.  
  137.     frame->set(x,y,width,-height);
  138.   }
  139.   else
  140.     gerror->report("Illegal window number");
  141. }
  142.  
  143. /******************************************************************
  144. * Sets the current drawing window.
  145. ******************************************************************/
  146. void  PC_Screen::set_current_window(int window_num)
  147. {
  148.   if (window_num > -1 && window_num < num_windows)
  149.   {
  150.     window_defined = TRUE;
  151.     current_window = window_num;
  152.     setviewport(win_data[current_window][0],      //resets viewport
  153.                 win_data[current_window][1],
  154.                 win_data[current_window][2],
  155.                 win_data[current_window][3],1);   //1=truncate output
  156.   }
  157.   else
  158.     gerror->report("Illegal window number");
  159. }
  160.  
  161. /******************************************************************
  162. * sets the current drawing color.  Call set_current_window() first!
  163. *----------------------------------------------------------------
  164. * Note from R. Gonzalez:  N. Gaskill had difficulty here because
  165. * the Turbo C++ file "graphics.h" conflicted with
  166. * my use of the identifier "color" and/or with my list of color
  167. * constants, as defined in my "color.h".  To get around this
  168. * he had to use "# define __COLORS" and was unable to take advan-
  169. * tage of the names given in "graphics.h".  I think he could have
  170. * gotten around this by momentarily #undef-ing my constants and
  171. * #define-ing constants with new names for the ones given in
  172. * graphics.h.  As it is, however, he is forced to use numerical
  173. * constants below.
  174. ******************************************************************/
  175. void  PC_Screen::set_pen_color(int color)
  176. {
  177.   if (window_defined)
  178.   {
  179.     switch (color)                                //correct colors for PC
  180.     {
  181.       case 0:  color = 0;
  182.         break;
  183.       case 1:  color = 15;
  184.         break;
  185.       case 2:  color = 4;
  186.         break;
  187.       case 3:  color = 14;
  188.         break;
  189.       case 4:  color = 2;
  190.         break;
  191.       case 5:  color = 1;
  192.         break;
  193.       case 6:  color = 3;
  194.         break;
  195.       case 7:  color = 5;
  196.         break;
  197.       default: break;
  198.     }
  199.     setcolor(color);                              //set draw color
  200.     setfillstyle(SOLID_FILL, color);              //set flood fill color
  201.     win_data[current_window][5] = color;          //save draw color 
  202.   }
  203.   else
  204.     gerror->report("Can't set color with no windows");
  205. }
  206.  
  207. /******************************************************************
  208. * Make the window the current color.   Call set_current_window() first!
  209. ******************************************************************/
  210. void  PC_Screen::fill_window(void)
  211. {
  212.   if (window_defined)
  213.     {
  214.         clearviewport();                              //clear for next
  215.         floodfill(2,2,LIGHTGRAY);                     //fill window
  216.     win_data[current_window][4] = getcolor();     //save fill color
  217.   }
  218.   else
  219.     gerror->report("Can't fill window with no windows");
  220. }
  221.  
  222. /******************************************************************
  223. * Move present pen position to new position using device
  224. * coordinates.  Call set_current_window() first!
  225. ******************************************************************/
  226. void  PC_Screen::move_to(Coord2* c)
  227. {
  228.   if (window_defined)
  229.     moveto((int) c->x,(int) c->y);
  230.   else
  231.     gerror->report("Can't move_to() with no windows");
  232. }
  233.  
  234. /******************************************************************
  235. * Draw from present pen position to new position using device
  236. * coordinates.  Call set_current_window() first!
  237. ******************************************************************/
  238. void  PC_Screen::draw_to(Coord2* c)
  239. {
  240.   if (window_defined)
  241.     lineto((int) c->x,(int) c->y);
  242.   else
  243.     gerror->report("Can't draw_to() with no windows");
  244. }
  245.  
  246. /******************************************************************
  247. * look for keyhit.
  248. ******************************************************************/
  249. void  PC_Screen::wait(void)
  250. {
  251.   printf("Press any key to exit");
  252.   getch();
  253. }
  254.  
  255. /******************************************************************
  256. * Destroy screen.
  257. ******************************************************************/
  258. PC_Screen::~PC_Screen(void)
  259. {
  260.   int window_num;
  261.   
  262.   closegraph(); 
  263. }
  264.  
  265.   
  266.